package com.wondertek.poms.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Strings;
import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.core.service.ISysDictTypeService;
import com.wondertek.poms.dao.po.SysDictType;
import com.wondertek.poms.dao.repo.ISysDictTypeDao;

/**
 * <p><b>SysDictTypeServiceImpl Description:</b> 字典类型表service层实现 </p>
 *
 * @author wzq
 * <p><b>DATE</b> 2019-08-13 11:02:01</p>
 */
@Service
@Scope("prototype")
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictType, Long> implements ISysDictTypeService {

    @Autowired
    private ISysDictTypeDao bcDictDao;

    @Override
    public Page<SysDictType> findAll(SysDictType entity, int page, int rows, String sord, String sidx) {
        Pageable pageable = getPageable(page - 1, rows, sidx, sord);
        Specification<SysDictType> specification = getWhereClause(entity);
        return bcDictDao.findAll(specification, pageable);
    }

    @Override
    @Transactional
    public SysDictType deleteLogic(Long id) {
        //TODO (如果报错删除方法体)
        SysDictType cp = jpaRepository.getOne(id);
        SysDictType.putTimeAndUser(cp);
        cp.setDeleted(Constants.DELETE_1);
        jpaRepository.save(cp);
        return cp;
    }

    @Override
    @Transactional
    public SysDictType deleteAndStopLogic(SysDictType dictType) {
        //TODO (如果报错删除方法体)
        SysDictType cp = jpaRepository.getOne(dictType.getId());
        SysDictType.putTimeAndUser(cp);
        cp.setDeleted(dictType.getDeleted());
        jpaRepository.save(cp);
        return cp;
    }

    @Override
    @Transactional
    public SysDictType merge(SysDictType model) {
        //TODO (如果报错删除报错行)
        SysDictType.putTimeAndUser(model);
        SysDictType dbo = jpaRepository.getOne(model.getId());
        CopyUtils.copyProperties(model, dbo);
        return jpaRepository.save(dbo);
    }

    @SuppressWarnings("serial")
    private Specification<SysDictType> getWhereClause(final SysDictType dictType) {
        return new Specification<SysDictType>() {

            @Override
            public Predicate toPredicate(Root<SysDictType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicate = new ArrayList<>();

                if (!Strings.isNullOrEmpty(dictType.getType())) {
                    predicate.add(cb.like(root.get("type"), "%" + dictType.getType() + "%"));
                }
                if (!Strings.isNullOrEmpty(dictType.getName())) {
                    predicate.add(cb.like(root.get("name"), "%" + dictType.getName() + "%"));
                }
                if (null != dictType.getDeleted()) {
                    predicate.add(cb.equal(root.get("deleted"), dictType.getDeleted()));
                }
                Predicate[] pre = new Predicate[predicate.size()];
                return query.where(predicate.toArray(pre)).getRestriction();
            }
        };
    }
}